﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agresso.Interface.Layer.Service;
using System.Timers;
using Agresso.Interface.Layer.Domain;

namespace Agresso.Interface.Layer.WinServiceTest
{
    class Program_2
    {
        private static System.Timers.Timer aTimer;
        private static ConfigurationSettingService m_confService;
        private static VesselService m_vesselService;
        private static CommissionInvoiceService m_CommissionInvoiceService;
        private static BunkerPOService m_BunkerPOService;
        private static BunkerGRNService m_BunkerGRNService;
        private static DateTime m_lastExecuteDate;
        delegate void Job();


        static void MainXX(string[] args)
        {
            aTimer = new System.Timers.Timer(15000);
            aTimer.Elapsed += new System.Timers.ElapsedEventHandler(aTimer_Elapsed);
            aTimer.Enabled = true;
            Console.WriteLine("Service started... at :" + DateTime.Now);
            Console.ReadLine();
        }
        static void aTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                Console.WriteLine("Service start check... at :" + DateTime.Now);
                m_confService = new ConfigurationSettingService();
                ConfigurationSetting cs = m_confService.GetConfigurationSetting("TCRelet");
                ExecuteJob(cs, new Job(ExecuteCommissionInvoiceTCReletType));
            }
            catch (Exception x)
            {
                Console.WriteLine(string.Format("The Update event at {0} Error : {1}", e.SignalTime, x.Message));
            }
        }
        private static void ExecuteJob(ConfigurationSetting cs, Job ex)
        {
            DateTime startDate = Convert.ToDateTime(cs.val_4);
            int everyD = Convert.ToInt16(cs.val_3);
            bool isEndDate = Convert.ToBoolean(cs.val_5);
            DateTime endDate = Convert.ToDateTime(cs.val_6);
            bool isRepeatTask = Convert.ToBoolean(cs.val_7);
            int everyTask = Convert.ToInt16(cs.val_8);
            int durationTask = Convert.ToInt16(cs.val_9);

            string timenow = DateTime.Now.Hour + ":" + DateTime.Now.Minute;
            string[] executeTime = cs.val_2.Split(':');
            DateTime executeDateTime = DateTime.Today.AddHours(Convert.ToInt16(executeTime[0])).AddMinutes(Convert.ToInt16(executeTime[1]));
            string executeTimeRepeat = "";

            if (DateTime.Today.Date.Subtract(startDate.Date).Days % everyD == 0)
            {
                if (isEndDate)
                {
                    if (DateTime.Today.Date <= endDate.Date)
                    {
                        if (timenow == cs.val_2)
                        {
                            ex();
                        }
                        else
                        {
                            if (isRepeatTask)
                            {
                                int times = durationTask / everyTask;
                                for (int i = 1; i <= times; i++)
                                {
                                    executeDateTime = executeDateTime.AddMinutes(everyTask);
                                    executeTimeRepeat = executeDateTime.Hour + ":" + executeDateTime.Minute;
                                    if (executeTimeRepeat == timenow)
                                    {
                                        ex();
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (timenow == cs.val_2)
                    {
                        ex();
                    }
                    else
                    {
                        if (isRepeatTask)
                        {
                            int times = durationTask / everyTask;
                            for (int i = 1; i <= times; i++)
                            {
                                executeDateTime = executeDateTime.AddMinutes(everyTask);
                                executeTimeRepeat = executeDateTime.Hour + ":" + executeDateTime.Minute;
                                if (executeTimeRepeat == timenow)
                                {
                                    ex();
                                }
                            }
                        }
                    }
                }
            }
        }
        private static void initService()
        {
            m_confService = new ConfigurationSettingService();
            m_vesselService = new VesselService();
            m_CommissionInvoiceService = new CommissionInvoiceService();
            m_BunkerPOService = new BunkerPOService();
            m_BunkerGRNService = new BunkerGRNService();
        }
        //private static void ExecuteInterface()
        //{
        //    initService();
        //    m_vesselService.ValidateVesselMaster();
        //    m_CommissionInvoiceService.ExecuteCommissionInvoiceTCReletType();
        //    m_CommissionInvoiceService.ExecuteCommissionInvoiceVCReletType();
        //    m_CommissionInvoiceService.ExecuteFreightHireInvoice();
        //    m_BunkerPOService.ExecuteBunkerProcurementPO();
        //    m_BunkerGRNService.ExecuteBunkerGRN();
        //}
        private static void ExecuteCommissionInvoiceTCReletType()
        {
            DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
            bool lastexecute = m_lastExecuteDate == time;
            if (!lastexecute)
            {
                m_lastExecuteDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                Console.WriteLine(string.Format("Execute Commission Invoice TCReletType Interface on {0}", DateTime.Now));
                initService();
                m_CommissionInvoiceService.ExecuteCommissionInvoiceTCReletType();
            }
        }
    }
}
